package com.yc.jpyy.ppapergenerating.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.ppapergenerating.dao.PpaperGeneratingDao;
import com.yc.jpyy.ppapergenerating.service.PpaperGeneratingService;
import com.yc.jpyy.utils.Tools;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class PpaperGeneratingServiceImpl extends BaseService implements PpaperGeneratingService {

	@Autowired
	private PpaperGeneratingDao ppaperGeneratingDao;

	@Override
	public int addPpaperGeneratingService(Map<String, Object> paramsMap) throws Exception {
		//查询排除当前ID之外有没有一样的组卷策略，为了和修改一样
		paramsMap.put("id", 0);
		int check = ppaperGeneratingDao.findPpaperGeneratingforAddDao(paramsMap);
		if(check > 0) {	
			return 0;
		}
		int ret = ppaperGeneratingDao.addPpaperGeneratingDao(paramsMap);
		if(ret > 0){
			//获得新增主键的ID
			int pgId = Integer.parseInt(paramsMap.get("id").toString());
			JSONObject jsonObject = JSONObject.fromObject(paramsMap);
			// 获取JSON对象中的JSON对象数组
			JSONArray jsonArray = jsonObject.getJSONArray("items");
			System.out.println(jsonArray);
			int jsonArraySize = jsonArray.size();// 获取数组长度
			for (int i = 0; i < jsonArraySize; i++) {
				// JSON对象转JSON字符串
				String str = jsonArray.getJSONObject(i).toString();
				// JSON字符串转map
				Map<String, Object> paramsMaps = Tools.JsonToMap(str);
				//将主键ID赋值给子表
				paramsMaps.put("pgid", pgId);
				ppaperGeneratingDao.addPpaperGeneratingDetailDao(paramsMaps);
			}
		}else{
			throw new Exception();
		}
        return ret;
	}

	@Override
	public int addPpaperGeneratingDetailService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return ppaperGeneratingDao.addPpaperGeneratingDetailDao(paramsMap);
	}

	@Override
	public int updatePpaperGeneratingService(Map<String, Object> paramsMap) throws Exception {
		int ret = 0 ;
		int check = ppaperGeneratingDao.findPpaperGeneratingforAddDao(paramsMap);
		if(check > 0) {	
			return 0;
		}		
		ret = ppaperGeneratingDao.updatePpaperGeneratingDao(paramsMap);
		if(ret > 0){
			JSONObject jsonObject = JSONObject.fromObject(paramsMap);
			// 获取JSON对象中的JSON对象数组
			JSONArray jsonArray = jsonObject.getJSONArray("items");
			int jsonArraySize = jsonArray.size();// 获取数组长度
			for (int i = 0; i < jsonArraySize; i++) {
				// JSON对象转JSON字符串
				String str = jsonArray.getJSONObject(i).toString();
				// JSON字符串转map
				Map<String, Object> paramsMaps = Tools.JsonToMap(str);
				//将主键ID赋值给子表
				ppaperGeneratingDao.updatePpaperGeneratingDetailDao(paramsMaps);
			}
		}else{
			throw new Exception();
		}
		return ret;
	}

	@Override
	public PageBean findPpaperGeneratingService(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		//HandlerDataSource.setDataSource("bizDataSource");
		List list = ppaperGeneratingDao.findPpaperGeneratingDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findPpaperGeneratingCountService(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}
	
	
	@Override
	public List<Map<String, Object>> findPpaperGeneratingToDetilService(Map<String, Object> paramsMap)
			throws DataAccessException {
		List<Map<String,Object>> paperList = ppaperGeneratingDao.findPpaperGenerating(paramsMap);
		if(paperList != null && paperList.size() > 0){
			for (Map<String, Object> maps : paperList) {
				paramsMap.put("ID", maps.get("ID").toString());
				maps.put("items",ppaperGeneratingDao.findPpaperGeneratingToDetilDao(paramsMap));
			}
		}
		return paperList;
	}

	@Override
	public List<Map<String, Object>> findProfessionCoursesService(Map<String, Object> paramsMap)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return ppaperGeneratingDao.findProfessionCoursesDao(paramsMap);
	}
	@Override
	public Map<String, Object> delPpaperGenerating(Map<String, Object> paramsMap)
			throws Exception {
		Map<String,Object> resultMap = new HashMap<>();
		paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
		//删除主表
		int count = ppaperGeneratingDao.delPpaperGenerating(paramsMap);
		if(count > 0){
			//删除对应子表
			int k = ppaperGeneratingDao.delPpaperGeneratingItem(paramsMap);
			if(k > 0){
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "删除成功");
			}else{
				throw new Exception();
			}
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		}
		return resultMap;
	}

	

	@Override
	public int findPpaperGeneratingCountService(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return ppaperGeneratingDao.findPpaperGeneratingDaoCount(paramsMap);
	}


}
